package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.List;

public class BlockingQueue {
    private List<Email> queue = new ArrayList<>();
    private boolean isInterrupted = false;

    public void add(Email email) {
        synchronized (queue) {
            queue.add(email);
            System.out.println("Email added to queue");
            queue.notifyAll();
        }
    }

    public Email remove() throws InterruptedException {
        synchronized (queue) {
            while (queue.isEmpty()) {
                if (isInterrupted) {
                    return null;
                }
                queue.wait();
            }
            return queue.remove(0);
        }
    }

    public boolean isEmpty() {
        synchronized (queue) {
            return queue.isEmpty();
        }
    }

    public void interrupt() {
        synchronized (queue) {
            isInterrupted = true;
            queue.notifyAll();
        }
    }
}
